import { app, Tray, Menu, BrowserWindow, nativeImage } from 'electron';
import path from 'path';

export function createTray(win: BrowserWindow): Tray {
  const iconPath = path.join(process.env.VITE_PUBLIC || '', 'logo.jpg');
  const tray = new Tray(nativeImage.createFromPath(iconPath));

  const contextMenu = [
    {
      label: '显示窗口',
      click: () => {
        win.show();
      },
    },
    {
      label: '退出',
      click: () => {
        win.destroy();
        app.quit();
      },
    },
  ]

  tray.setToolTip('微信');
  tray.setContextMenu(
    Menu.buildFromTemplate(contextMenu)
  );

  // 点击托盘图标，显示主窗口
  tray.on("click", () => {
    win.setSkipTaskbar(false)
    win.show();
  })

  // 双击打开
  // tray.on('double-click', () => {
  //   if (win.isMinimized()) {
  //     win.restore();
  //   }
  //   win.show();
  // });

  return tray;
}
